package com.cdvtc.flower.web.servlet;

import com.cdvtc.flower.dao.OrderDao;
import com.cdvtc.flower.dao.ProductsDao;
import com.cdvtc.flower.dao.impl.OrderDaoImpl;
import com.cdvtc.flower.dao.impl.ProductsDaoImpl;
import com.cdvtc.flower.model.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/order-now")
public class OrderNowServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Customer customer = (Customer) request.getSession().getAttribute("customer");

        if (customer != null) {
            String productId = request.getParameter("product_id");
            System.out.println("购物车获取ID"+Integer.parseInt(productId));
            int productQuantity = Integer.parseInt(request.getParameter("quantity"));
            System.out.println("获取到的数量"+productQuantity);
            if (productQuantity <= 0) {
                productQuantity = 1;
            }
            Order order = new Order();

            LocalDateTime localDateTime = LocalDateTime.now();
            localDateTime.toInstant(ZoneOffset.of("+8"));
            System.out.println("订单生成时间"+localDateTime);
            ProductsDao productsDao = new ProductsDaoImpl();
            Products products =  productsDao.getProduct(Integer.parseInt(productId));
            System.out.println("商品信息"+products);
            Orderdetail orderdetail = new Orderdetail();

            orderdetail.setProduct(products);

            orderdetail.setSale_price(products.getSale_price()); //复制商品价格
            orderdetail.setQuantity(productQuantity);
//            orderdetail.setOrder_id(1);
//            orderdetail.setOrder_detail_id(1);
//            orderdetail.setCustomer_id(customer.getCustomer_id());
//            orderdetail.setOrder_date_time(new Date(new java.util.Date().getTime()));
//            orderdetail.setReview("一般");
            List<Orderdetail> orderdetailList = new ArrayList<>();
            orderdetailList.add(orderdetail);

            order.setOrderdetailList(orderdetailList);

            System.out.println(orderdetailList);

            order.setCustomer(customer);

            OrderDao orderDao = new OrderDaoImpl();
            boolean result = orderDao.insertOrder(order);
            if (result) {
                ArrayList<CartItem> cart = (ArrayList<CartItem>) request.getSession().getAttribute("cart");
                if (cart != null) {
                    for (CartItem item : cart) {
                        if (item.getProduct().getProduct_id() == Integer.parseInt(productId)) {
                            cart.remove(cart.indexOf(item));  // 清空购物车中的对应明细项
                            break;
                        }
                    }
                }
                response.sendRedirect("orders.jsp");
            } else {
                out.println("订单生成失败！");
            }
        } else {
            response.sendRedirect("login.jsp");
        }
    }
}
